package view

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"miaoyu/internal/library/resp"
	"miaoyu/internal/service"
)

/*
@Time : 2023/6/20 10:37
@Author : 56766
@File : uniResp.logic.go
*/
func init() {
	uni := sUniResp{}
	service.RegisterUniResp(&uni)
}

type sUniResp struct {
	jsonResp *resp.JsonData
	tplResp  *resp.TplData
}

func (s *sUniResp) UniResp(r *ghttp.Request, message string, path string) {
	fmt.Println("uniresp", r.IsAjaxRequest())
	if r.IsAjaxRequest() {
		s.jsonResp = service.JsonResp().CommonResp(r)
		s.jsonResp.SetNoLoginJsonExit(message)
	} else {
		s.tplResp = service.TplResp().BuildTpl(r)
		s.tplResp.WriteTplWithExit(resp.BackendJumpLayout, g.MapStrAny{
			"msg":  message,
			"path": path,
		})
	}
}
func (s *sUniResp) UniNoLoginResp(r *ghttp.Request, message string, path string) {
	s.jsonResp = service.JsonResp().CommonResp(r)
	s.jsonResp.SetNoLoginJsonExit(message)
	//if r.IsAjaxRequest() {
	//	s.jsonResp = service.JsonResp().CommonResp(r)
	//	s.jsonResp.SetNoLoginJsonExit(message)
	//} else {
	//	s.tplResp = service.TplResp().BuildTpl(r)
	//	s.tplResp.WriteTplWithExit(resp.BackendJumpLayout, g.MapStrAny{
	//		"msg":  message,
	//		"path": path,
	//	})
	//
	//}
}

func RespJavascriptFile(r *ghttp.Request, content string) {
	r.Response.Header().Set("Content-Type", "text/javascript; charset=utf-8")
	r.Response.WriteExit(content)
}
